/*
* Description: 存储进程生命周期信息
*/
#ifndef PROCESS_DETECT_H
#define PROCESS_DETECT_H
#include <Windows.h>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <deque>
#include <mutex>
#include <ctime>
#include <unordered_set>
#include "singleton.h"
#include "rpc_service/sendto_host.h"

namespace DFX {

struct ProcessLifeRecord {
    std::wstring processName = L"";
    DWORD processId = 0;
    // 进程启动时间
    time_t processCreateTime = 0;
    // 进程关闭时间
    time_t processCloseTime = 0;
};

enum ProcessOperation {
    PROCESS_CREATE = 0,
    PROCESS_CLOSE
};

class ProcessDetect {
public:
    ProcessDetect() = default;
    ~ProcessDetect() = default;
    void Init(std::shared_ptr<WINCONTROL::SendToHost> sendtoHost);
    void Deinit();
    void Run();
    void Stop();
    void OnRecvProcessMsg(DWORD processId, ProcessOperation type);
    void SaveProcessInfo(DWORD processId, std::string processName, ProcessOperation operation);
    void ClearExpireData();
    std::vector<ProcessLifeRecord> GetProcessLifeInfo();

private:
    void SetToVector(const ProcessLifeRecord& objProcessLifeRecord);
    void SetProcessCloseTime(DWORD pid, const std::wstring& processName);
    void GetMonitorProcessInfo();
    std::string GetProcessNameFromFullPath(const std::string& path);
    std::string ReadAppUsingMonitorList();
    void SyncWinUpdateProcStatus(std::string processName, ProcessOperation type);

    std::vector<ProcessLifeRecord> vectorProcessLifeRecord = {};
    std::mutex objectlock = {};
    std::unordered_set<std::string> monitorProcess = {};
    std::mutex procNameInfoMutex;
    std::map<DWORD, std::string> procNameInfo;
    std::mutex monitorProcessMtx = {};
    std::shared_ptr<WINCONTROL::SendToHost> m_sendtoHost{ nullptr };
};

#define PROCESSDETECT SingletonNew<DFX::ProcessDetect>::GetInstance()
}  // namespace DFX
#endif